package com.rtype.GPSilencer.helpers;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;

public class TriggerDbAdapter extends BaseDbAdapter {

	private static final String DB_TABLE = "trigger_location";
	private static final String KEY_ROWID = "_id";
	private static final String KEY_LATITUDE = "latitude";
	private static final String KEY_LONGITUDE = "longitude";
	private static final String KEY_RADIUS = "radius";

	public TriggerDbAdapter(Context ctx) {
		super(ctx);
	}
	
	public long create(String latitude, String longitude, float radius) {
		ContentValues values = new ContentValues();
		values.put(KEY_LATITUDE, latitude);
		values.put(KEY_LONGITUDE, longitude);
		values.put(KEY_RADIUS, radius);
		return mDb.insert(DB_TABLE, null, values);
	}
	
	public boolean delete(long rowId) {
		return mDb.delete(DB_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}
	
	public Cursor fetchAll() {
		return mDb.query(DB_TABLE, new String[] {KEY_ROWID, KEY_LATITUDE,
                KEY_LONGITUDE, KEY_RADIUS}, null, null, null, null, null);
	}
	
	public Cursor fetch(long rowId) throws SQLException {
		Cursor mCursor =

            mDb.query(true, DB_TABLE, new String[] {KEY_ROWID, KEY_LATITUDE,
                    KEY_LONGITUDE, KEY_RADIUS}, KEY_ROWID + "=" + rowId, 
                    null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
	}

	public boolean update(long rowId, String latitude, String longitude, float radius) {
		ContentValues values = new ContentValues();
		values.put(KEY_LATITUDE, latitude);
		values.put(KEY_LONGITUDE, longitude);
		values.put(KEY_RADIUS, radius);
        return mDb.update(DB_TABLE, values, KEY_ROWID + "=" + rowId, null) > 0;
	}

}
